﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using ComicIsland.Common;
using ComicIsland.Common.Entities;

/**
 * 创建时间：2014/12/18 17:15:55
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.DownloadConverter
{
    /// <summary>
    /// 某个漫画中已经下载完成的章节数
    /// </summary>
    public class ComicChapterFinishedTotalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                string format = parameter.ToString();
                int currentValue = 0;
                if (value != null)
                {
                    int.TryParse(value.ToString(), out currentValue);
                }
                var items = App.comicDbHelper.ChapterGetObservableCollectionByComic(currentValue);
                if (items != null && items.Count > 0)
                {
                    currentValue = items.Where(p => p.DownloadStatus.Equals(DownloadComicChapterStatus.Completed.ToString())).Count();                    
                }
                return string.Format(format, currentValue);
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
